home *** CD-ROM | disk | FTP | other *** search
/ 3D Game Programming All in One / 3D Game Programming All in One Disc.iso / 3D2E / RESOURCES / CH2 / TwotyFruity.cs < prev    next >
Text File  |  2006-09-11  |  4KB  |  121 lines

  1. // ========================================================================
  2. //  TwotyFruity.cs
  3. //
  4. //  This program adds up the costs and quantities of selected fruit types
  5. //  and outputs the results to the display. This module is a variation
  6. //  of the the FruitLoopy.cs module designed to demonstrate how to use
  7. //  functions.
  8. // ========================================================================
  9.  
  10. function initializeFruit( )
  11. // ------------------------------------------------------------------------
  12. //     Set the starting values for our fruit arrays, and the type
  13. //     indices
  14. //
  15. //     RETURNS: number of different types of fruit
  16. //
  17. // ------------------------------------------------------------------------
  18. {
  19.     %numFruitTypes = 5; // so we know how many types are in our arrays
  20.     $bananaIdx=0;    // initilize the values of our index variables
  21.     $appleIdx=1;
  22.     $orangeIdx=2;
  23.     $mangoIdx=3;
  24.     $pearIdx=4;
  25.  
  26.     $names[$bananaIdx] = "bananas"; // initilize the fruit name values
  27.     $names[$appleIdx] = "apples";
  28.     $names[$orangeIdx] = "oranges";
  29.     $names[$mangoIdx] = "mangos";
  30.     $names[$pearIdx] = "pears";
  31.  
  32.     $cost[$bananaIdx] = 1.15; // initilize the price values
  33.     $cost[$appleIdx] = 0.55;
  34.     $cost[$orangeIdx] = 0.55;
  35.     $cost[$mangoIdx] = 1.90;
  36.     $cost[$pearIdx] = 0.68;
  37.  
  38.     $quantity[$bananaIdx] = 1; // initilize the quantity values
  39.     $quantity[$appleIdx]  = 3;
  40.     $quantity[$orangeIdx] = 4;
  41.     $quantity[$mangoIdx]  = 1;
  42.     $quantity[$pearIdx]   = 2;
  43.  
  44.     return(%numFruitTypes);
  45. }
  46.  
  47. function addEmUp(%numFruitTypes)
  48. // ------------------------------------------------------------------------
  49. //     Add all prices of different fruit types to get a full total cost
  50. //
  51. //     PARAMETERS: %numFruitTypes ûthe number of different fruit that are tracked
  52. //
  53. //     RETURNS: total cost of all fruit
  54. //
  55. // ------------------------------------------------------------------------
  56. {
  57.    %total = 0;
  58.    for (%index = 0; %index < %numFruitTypes; %index++)
  59.    {
  60.       %total = %total + ($quantity[%index]*$cost[%index]);
  61.    }
  62.    return %total;
  63. }
  64.  
  65.  
  66. // ------------------------------------------------------------------------
  67. //  countEm
  68. //
  69. //     Add all quantities of different fruit types to get a full total
  70. //
  71. //     PARAMETERS: %numFruitTypes ûthe number of different fruit that are tracked
  72. //
  73. //     RETURNS: total of all fruit types
  74. //
  75. // ------------------------------------------------------------------------
  76. function countEm(%numFruitTypes)
  77. {
  78.    %total = 0;
  79.    for (%index = 0; %index < %numFruitTypes; %index++)
  80.    {
  81.       %total = %total + $quantity[%index];
  82.    }
  83.    return %total;
  84. }
  85.  
  86. function runTwotyFruity()
  87. // ------------------------------------------------------------------------
  88. //     Entry point for program. This program adds up the costs
  89. //     and quantities of selected fruit types and outputs the results to
  90. //     the display. This program is a variation of the program FruitLoopy
  91. //
  92. // ------------------------------------------------------------------------
  93. {
  94.    //
  95.    // ----------------- Initialization ---------------------
  96.    //
  97.  
  98.    %numFruitTypes=InitializeFruit(); // set up fruit arrays and variables
  99.    %numFruit=0;     // always a good idea to initialize *all* variables!
  100.    %totalCost=0;    // (even if we know we are going to change them later)
  101.  
  102.    //
  103.    // ----------------- Computation ---------------------
  104.    //
  105.  
  106.    // Display the known statistics of the fruit collection
  107.    for (%index = 0; %index < %numFruitTypes; %index++)
  108.    {
  109.    echo("Cost of " @ $names[%index] @ ":$" @ $cost[%index]);
  110.    echo("Number of " @ $names[%index] @ ":" @ $quantity[%index]);
  111.    }
  112.  
  113.    // count up all the pieces of fruit, and display that result
  114.    %numFruit = countEm(%numFruitTypes);
  115.    echo("Total pieces of Fruit:" @ %numFruit);
  116.  
  117.    // now calculate the total cost
  118.    %totalCost = addEmUp(%numFruitTypes);
  119.    echo("Total Price of Fruit:$" @ %totalCost);
  120. }
  121.